<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace AlibabaCloud\Client\Result;

class Result extends \GuzzleHttp\Psr7\Response implements \ArrayAccess, \IteratorAggregate, \Countable
{
    use \AlibabaCloud\Client\Traits\HasDataTrait;
    protected $request;
    public function __construct(\Psr\Http\Message\ResponseInterface $response, \AlibabaCloud\Client\Request\Request $request = NULL)
    {
        parent::__construct($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase());
        $this->request = $request;
        $this->resolveData();
    }
    private function resolveData()
    {
        $content = $this->getBody()->getContents();
        $this->getRequestFormat();
        switch ($this->getRequestFormat()) {
            case "JSON":
                $result_data = $this->jsonToArray($content);
                break;
            case "XML":
                $result_data = $this->xmlToArray($content);
                break;
            case "RAW":
                $result_data = $this->jsonToArray($content);
                break;
            default:
                $result_data = $this->jsonToArray($content);

        }
        if(!$result_data) {
            $result_data = [];
        }
        $this->dot($result_data);
    }
    private function getRequestFormat()
    {
        return $this->request instanceof \AlibabaCloud\Client\Request\Request ? strtoupper($this->request->format) : "JSON";
    }
    private function jsonToArray($response)
    {
        try {
            return json_decode($response, true);
        } catch (\InvalidArgumentException $exception) {
            return [];
        }
    }
    private function xmlToArray($string)
    {
        try {
            return json_decode(json_encode(simplexml_load_string($string)), true);
        } catch (\Exception $exception) {
            return [];
        }
    }
    public function __toString()
    {
        return (string) $this->getBody();
    }
    public function getRequest()
    {
        return $this->request;
    }
    public function getResponse()
    {
        return $this;
    }
    public function isSuccess()
    {
        return 200 <= $this->getStatusCode() && $this->getStatusCode() < 300;
    }
}

?>